// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import ItemFrame from "../item/itemFrame";
import {globalConfig} from "../config/globalConfig";
import {getReward} from "../config/config";

const {ccclass, property} = cc._decorator;

@ccclass
export default class LuckyTurntable extends cc.Component {

  @property(cc.Node)
  rotateNode: cc.Node = null

  @property(cc.Node)
  rewardsNode: cc.Node = null;

  private rewardsItem: Array<cc.Node> = []

  private tweenNode: cc.Node = new cc.Node()

  onLoad() {
    this.rewardsItem = this.rewardsNode.children
    const luckyTurntable = globalConfig.turntable
    this.rewardsItem.forEach((value, index) => {
      const config: any = luckyTurntable[index]
      const rewardConfig: any = getReward(config.reward)
      value.getComponent(ItemFrame).setItemData(rewardConfig.itemid, rewardConfig.number, rewardConfig.type)
    })
  }

  private rotateSpeed = 0;

  update(dt) {
    this.rotateSpeed = this.tweenNode.x
    this.rotateNode.rotation += this.rotateSpeed * dt
    this.checkStop();
  }

  async fastRotate(indexes: Array<number>) {
    await this.fastRotateOnceTo(indexes[0])
    // for (let i = 0; i < indexes.length; i++) {
    //   await this.fastRotateOnceTo(indexes[i])
    //   await this.wait(300)
    // }
  }

  rotateOnceTo(index: number) {
    const rotation = this.getRotationFromIndex(index)
    const action = cc.sequence(
      cc.moveTo(2, cc.v2(1500, 0)),
      cc.delayTime(0.5),
      cc.moveTo(2, cc.v2(100, 0)),
      cc.callFunc(() => {
        this.setStopRotation(rotation)
      })
    )
    this.tweenNode.runAction(action)
    return new Promise(resolve => {
      this.node.once('onStop', function () {
        resolve()
      })
    })
  }

  private fastRotateOnceTo(index: number) {
    return new Promise((resolve, reject) => {
      const rotation = this.getRotationFromIndex(index)
      const action = cc.sequence(
        cc.moveTo(0.8, cc.v2(1500, 0)),
        cc.delayTime(0.1),
        cc.moveTo(0.1, cc.v2(1000, 0)),
        cc.callFunc(() => {
          this.setStopRotation(rotation)
        })
      )
      this.tweenNode.runAction(action)
      this.node.once('onStop', function () {
        resolve()
      })
    })
  }

  private getRotationFromIndex(index: number) {
    return (index - 1) * 45 + 22.5
  }

  private isCheckStop = false
  private stopRotation = 0;

  private setStopRotation(stopRotation: number) {
    this.isCheckStop = true
    this.stopRotation = stopRotation
  }

  private stopCallback() {
    this.rotateNode.rotation = this.stopRotation
    this.tweenNode.stopAllActions()
    this.tweenNode.x = 0
    this.isCheckStop = false
    this.node.emit('onStop')
  }

  private checkStop() {
    const rotation = parseFloat((this.rotateNode.rotation % 360).toFixed(1))
    if (this.isCheckStop && rotation < this.stopRotation + 10 && rotation > this.stopRotation - 10) {
      this.stopCallback();
    }
  }

  private wait(time: number) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  }
}
